function getWXStatusHeight() {
	// 获取距上
	const barTop = wx.getSystemInfoSync().statusBarHeight
	// 获取胶囊按钮位置信息
	const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
	// 获取导航栏高度
	const barHeight = menuButtonInfo.height + (menuButtonInfo.top - barTop) * 2 + 20
	let barWidth = menuButtonInfo.width

	const menuHeight = 44 + barTop
	return {
		barHeight,
		barTop,
		barWidth,
		menuHeight
	}

}

/**
 * 获取权限
 * @param {string} params.permission  对应权限的scope，例如 scope.bluetooth
 * @param {String} params.permissionName  提示框显示的权限名称，例如 蓝牙
 * @param {String} params.customPopup  将实现自定义的弹窗代替系统弹窗
 * @param {Function} params.success  授权成功回调函数
 * @param {Function} params.fail  授权失败回调函数
 * @param {Function} params.cancel  取消继续授权回调函数
 */
function getPermission(params) {

	const _permission = `scope.${params.permission}`
	const _tipsContent = `您拒绝了${params.permissionName}权限，将导致部分功能不能正常使用，去设置权限？`

	uni.getSetting({
		success(res) {
			// 判断是否有相关权限属性
			if (res.authSetting.hasOwnProperty(_permission)) {
				// 属性存在，且为false
				if (!res.authSetting[_permission]) {
					uni.showModal({
						title: '提示',
						content: _tipsContent,
						success: (res) => {
							if (res.confirm) {
								uni.openSetting()
							} else {
								if (params.cancel) {
									setTimeout(() => {
										params.cancel()
									}, 200)
								}
							}
						}
					})
				} else {
					params.success && params.success()
				}
			} else {
				// 属性不存在，需要授权
				uni.authorize({
					scope: _permission,
					success() {
						// 授权成功
						params.success && params.success()
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: _tipsContent,
							success: (res) => {
								if (res.confirm) {
									uni.openSetting()
								} else {
									if (params.cancel) {
										setTimeout(() => {
											params.cancel()
										}, 200)
									}
								}
							}
						})
					}
				})
			}
		}
	})
}


const showToast = msg => {
	uni.showToast({
		title: msg,
		duration: 2000,
		position: 'center',
		icon: 'none'
	})
}

/**
 * 判断权限，执行函数
 */
const doSomeSthingByLogin = (userLoginRef, callback, ...args) => {
	console.log(userLoginRef, "userLoginRef");
	if (!uni.getStorageSync('token') || (uni.getStorageSync('userInfo') == "null" || !uni.getStorageSync(
			"userInfo"))) {
		userLoginRef.showLogin = true
	} else {
		const userInfo = uni.getStorageSync('userInfo')
		if (userInfo.isBindMobile == 0) {
			userLoginRef.showBinding()
		}
		callback(...args)
	}
}
const saveing = false

const saveVideoFile = () => {
	return new Promise((resolve, reject) => {

		const videoObj = uni.getStorageSync("videoFile");

		if (videoObj) {
			// 判断时间有没有过期
			const now = new Date();
			// 检查是否过期
			if (now.getTime() < videoObj.time) {
				resolve(videoObj.path);
				return;
			} else {
				// 如果过期，移除该数据并返回 null
				uni.removeStorageSync("videoFile");
			}
		}

		if (!saveing) resolve(null)

		uni.downloadFile({
			url: 'https://fomoclub.top/resource/mp4/1.mp4', // 视频的网络地址
			success: function(res) {
				const tempFilePath = res.tempFilePath;
				uni.getFileSystemManager().saveFile({
					tempFilePath: tempFilePath,
					success: function(res) {
						const savedFilePath = res.savedFilePath;
						const now = new Date();
						const expiryTime = now.getTime() + 24 * 60 * 60 * 1000;
						uni.setStorageSync("videoFile", {
							time: expiryTime,
							path: savedFilePath
						});
						resolve(savedFilePath); // 返回保存的文件路径
					},
					fail: function(err) {
						console.error('保存失败', err);
						reject(err);
					}
				})

			},
			fail: function(err) {
				console.error('下载失败', err);
				reject(err);
			}
		});
	});
};

// 获取时间YYYY-mm-dd
const getDateForYmd = () => {
	const date = new Date()
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	return `${year}-${month}-${day}`
}

function debounce(fn, delay) {
	return function() {
		let context = this,
			args = arguments
		clearTimeout(fn.timer)
		fn.timer = setTimeout(function() {
			fn.apply(context, args)
		}, delay)
	}
}

import {
	app
} from "main"

// 本地图片
function getImgUrlLocal(url) {
	return '/static' + url
}

function getImgUrl(url) {
	return app.$baseImgUrl + url
}

module.exports = {
	getImgUrlLocal,
	getImgUrl,
	getWXStatusHeight,
	getPermission,
	showToast,
	doSomeSthingByLogin,
	debounce,
	saveVideoFile,
	getDateForYmd
}
